home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: in1.uu.net!shore!mv!usenet
- From: ENGR@GSSI.MV.COM (Michael Furman)
- Subject: Re: Constructor member initializer list
- Message-ID: <DpCE37.DpE@mv.mv.com>
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- Organization: GSSI
- Date: Thu, 4 Apr 1996 14:40:18 GMT
- References: <4jua8k$fa1$2@mhadg.production.compuserve.com>
- X-Newsreader: WinVN 0.99.7
- X-Nntp-Posting-Host: gssi.mv.com
-
- In article <4jua8k$fa1$2@mhadg.production.compuserve.com>,
- 74312.2300@CompuServe.COM says...
- >
- >How do you initialize class members in the initializer list if the
- >member is a structure?
-
- I think you mean aggregate class - not structure. There in no difference
- between structure and class in C++ except default "public:"
- at the begining of structure. Aggregate class is a class with no
- constructors, private or protected members, base classes or virtual
- functions.
-
- >
- >Consider the following code fragment.
- >
- >typedef struct _tagRect {
- > int x;
- > int y;
- > int w;
- > int h;
- >} RECTANGLE;
- >
- >class Foo {
- > Foo() : <how do I initialize fooRect and it's members> {};
- > RECTANGLE fooRect;
- >};
-
- You can not do it if RECTANGLE is an aggregate, because constructor
- initializer
- can not use form {....}. You have two choices: add constructor to your
- structure or put initialization inside constructor body using assignment
- statements.
-
- And your typedef is redundant - you can write:
-
- //=== Variant 1
- struct RECTANGLE {
- int x;
- int y;
- int w;
- int h;
- };
-
- class Foo {
- RECTANGLE fooRect;
- Foo() {foorect.x = 1; ........ }
- };
-
- //===== or:
- //=== Variant 2
- struct RECTANGLE {
- int x;
- int y;
- int w;
- int h;
- RECTANGLE(int x_, int y_, int w_, int h_) : x(x_), y(y_), w(w_), h(h_) {}
- };
-
- class Foo {
- RECTANGLE fooRect;
- Foo() : fooRect(1, 2, 3, 4) {}
- };
-
- I did not test this code fragments - they may contain tupos.
-
- --
- <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
- ---------------------------------------------------------------
- Michael Furman, (603)893-1109
- Geophysical Survey Systems, Inc. fax:(603)889-3984
- 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- North Salem, NH 03073-0097 71543.1334@compuserve.com
- ---------------------------------------------------------------
-
-